package com.example.iorder.util;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.example.iorder.R;
import com.example.iorder.SearchRestaurant;
import com.example.iorder.reservation.read.Activity_ShowAllReservations;
import com.example.iorder.user.*;


public class NavigationManager 
{
	private static View footerView;
	
	public static void addHeaderNavigation(Context context, RelativeLayout layoutToAddTo) 
	{
		footerView = LayoutInflater.from(context).inflate(R.layout.header_navigation, null);
		
		// Configure the buttons in the tab
		Button myReservationsButton = (Button) footerView.findViewById(R.id.nav_myReservationButton);
		myReservationsButton.setTag(TabButtons.MyReservations);
		Intent reservationIntent = new Intent(context, Activity_ShowAllReservations.class);
		
		Button searchButton = (Button) footerView.findViewById(R.id.nav_searchButton);
		searchButton.setTag(TabButtons.Search);
		Intent searchIntent = new Intent(context, SearchRestaurant.class);
		
		Button loginButton = (Button) footerView.findViewById(R.id.nav_loginButton);
		loginButton.setTag(TabButtons.Login);
		Intent loginIntent = new Intent(context, LoginUser.class);		
		
		ArrayList<Button> unpressedButtonForReservation = new ArrayList<Button>();
		unpressedButtonForReservation.add(searchButton);
		unpressedButtonForReservation.add(loginButton);
		addIntentToTabButton(myReservationsButton, context, reservationIntent, unpressedButtonForReservation);
		
		ArrayList<Button> unpressedButtonForSearch = new ArrayList<Button>();
		unpressedButtonForSearch.add(myReservationsButton);
		unpressedButtonForSearch.add(loginButton);
		addIntentToTabButton(searchButton, context, searchIntent, unpressedButtonForSearch);
		
		ArrayList<Button> unpressedButtonForLogin = new ArrayList<Button>();
		unpressedButtonForLogin.add(myReservationsButton);
		unpressedButtonForLogin.add(searchButton);
		addIntentToTabButton(loginButton, context, loginIntent, unpressedButtonForLogin);		
		
		// Add the view to the layout
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, footerView.getId());
		layoutToAddTo.addView(footerView, lp);
	}
	
	public static boolean setCurrentTab(Activity context, TabButtons tab) {
		//footerView = LayoutInflater.from(context).inflate(R.layout.footer_navigation, null);
		footerView = context.findViewById(R.id.footer_tabs);
		if (footerView != null) 
		{
			Button buttonToSetPressed = null;
			Button buttonToSetUnpressed = null;
			ArrayList<Button> buttonsToSetUnpressed = new ArrayList<Button>();
			
			switch (tab) 
			{
				case MyReservations: 
					buttonToSetPressed = (Button) footerView.findViewById(R.id.nav_myReservationButton);
					buttonToSetPressed.setTag(TabButtons.MyReservations);
					
					buttonToSetUnpressed = (Button) footerView.findViewById(R.id.nav_searchButton);
					buttonToSetUnpressed.setTag(TabButtons.Search);
					buttonsToSetUnpressed.add(buttonToSetUnpressed);
						
					buttonToSetUnpressed = (Button) footerView.findViewById(R.id.nav_loginButton);
					buttonToSetUnpressed.setTag(TabButtons.Login);
					buttonsToSetUnpressed.add(buttonToSetUnpressed);					
					
					break;
					
				case Search: 
					buttonToSetPressed = (Button) footerView.findViewById(R.id.nav_searchButton);
					buttonToSetPressed.setTag(TabButtons.Search);
					
					buttonToSetUnpressed = (Button) footerView.findViewById(R.id.nav_myReservationButton);
					buttonToSetUnpressed.setTag(TabButtons.MyReservations);
					buttonsToSetUnpressed.add(buttonToSetUnpressed);

					buttonToSetUnpressed = (Button) footerView.findViewById(R.id.nav_loginButton);
					buttonToSetUnpressed.setTag(TabButtons.Login);
					buttonsToSetUnpressed.add(buttonToSetUnpressed);	
					break;
					
				case Login: 
					buttonToSetPressed = (Button) footerView.findViewById(R.id.nav_loginButton);
					buttonToSetPressed.setTag(TabButtons.Login);
					
					buttonToSetUnpressed = (Button) footerView.findViewById(R.id.nav_myReservationButton);
					buttonToSetUnpressed.setTag(TabButtons.MyReservations);
					buttonsToSetUnpressed.add(buttonToSetUnpressed);	
					
					buttonToSetUnpressed = (Button) footerView.findViewById(R.id.nav_searchButton);
					buttonToSetUnpressed.setTag(TabButtons.Search);
					buttonsToSetUnpressed.add(buttonToSetUnpressed);					
					
					break;
					
			}
			
			if (buttonToSetUnpressed != null)
				buttonsToSetUnpressed.add(buttonToSetUnpressed);
			if (buttonToSetPressed != null) {
				setButtonAsPressed(buttonToSetPressed, context, tab, buttonsToSetUnpressed);
				return true;
			}
		}
		return false;
	}
	
	private static void addIntentToTabButton(Button buttonToSetPressed, Context context, Intent intent, ArrayList<Button> buttonsToSetUnpressed) {
		final Intent i = intent;
		final Context c = context;
		final ArrayList<Button> buttons = buttonsToSetUnpressed;
		buttonToSetPressed.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				// Change the style of the pressed button to pressed
				Button pressedButton = (Button) view;
				TabButtons buttonTag = (TabButtons) pressedButton.getTag();
				setButtonAsPressed(pressedButton, c, buttonTag, buttons);
				
				// Start the activity if it's not already running
				String fullNameclassToLaunch = i.resolveActivity(c.getPackageManager()).getClassName();
				String fullNameclassRunning = c.getClass().toString();
				int lastDotIndex = fullNameclassToLaunch.lastIndexOf(".");
				String classToLaunch = fullNameclassToLaunch.substring(lastDotIndex+1, fullNameclassToLaunch.length());
				lastDotIndex = fullNameclassRunning.lastIndexOf(".");
				String classRunning = fullNameclassRunning.substring(lastDotIndex+1, fullNameclassRunning.length());
				
				if (!classToLaunch.equals(classRunning)) {
					c.startActivity(i);
					//((Activity)c).finish();
				}
			}
		});
	}
	
	@SuppressWarnings("deprecation")
	private static void setButtonAsPressed(Button pressedButton, Context c, TabButtons type, ArrayList<Button> buttonsToSetUnpressed) 
	{
		Drawable backgroundForPressedButton = c.getResources().getDrawable(R.drawable.tab_main_pressed);
		Drawable imageForPressedButton = getPressedImageTabButtonsType(c, type);
		pressedButton.setBackgroundDrawable(backgroundForPressedButton);
		
		if (imageForPressedButton != null)
			pressedButton.setCompoundDrawablesWithIntrinsicBounds(null, imageForPressedButton, null, null);
		
		pressedButton.setTextColor(Color.WHITE);
		pressedButton.setCompoundDrawablePadding(5);
		pressedButton.setPadding(5, 5, 5, 5);
		
		// Change the style of all other buttons to unpressed
		for (Button unpressedButton : buttonsToSetUnpressed) 
		{
			TabButtons buttonTag2 = (TabButtons)unpressedButton.getTag();
			setButtonAsUnpressed(unpressedButton, c, buttonTag2);
		}
	}
	
	@SuppressWarnings("deprecation")
	private static void setButtonAsUnpressed(Button unpressedButton, Context c, TabButtons type) {
		Drawable backgroundForUnPressedButton = c.getResources().getDrawable(R.drawable.tab_main_unpressed);
		Drawable imageForUnPressedButton = getUnPressedImageTabButtonsType(c, type);
		unpressedButton.setBackgroundDrawable(backgroundForUnPressedButton);
		if (imageForUnPressedButton != null)
			unpressedButton.setCompoundDrawablesWithIntrinsicBounds(null, imageForUnPressedButton, null, null);
		unpressedButton.setTextColor(c.getResources().getColor(R.color.dark_gray));
		unpressedButton.setCompoundDrawablePadding(5);
		unpressedButton.setPadding(5, 5, 5, 5);
	}
	
	private static Drawable getPressedImageTabButtonsType(Context c, TabButtons type) 
	{
		Drawable matchingImage = null;
		switch (type) {
			case MyReservations: matchingImage = c.getResources().getDrawable(R.drawable.list); break;
			case Search: matchingImage = c.getResources().getDrawable(R.drawable.search_icon2); break;
			case Login: matchingImage = c.getResources().getDrawable(R.drawable.login_white_icon); break;
		}
		return matchingImage;
	}
	
	private static Drawable getUnPressedImageTabButtonsType(Context c, TabButtons type) 
	{
		Drawable matchingImage = null;
		switch (type) 
		{
			case MyReservations: matchingImage = c.getResources().getDrawable(R.drawable.list_darkgray); break;
			case Search: matchingImage = c.getResources().getDrawable(R.drawable.search_icon2_darkgray); break;
			case Login: matchingImage = c.getResources().getDrawable(R.drawable.login_grey_icon); break;

		}
		return matchingImage;
	}
	
	public enum TabButtons {
		MyReservations,
		Search,
		Login
	}
}
